package com.xxx.video.commen.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.core.content.FileProvider;


import com.xxx.video.commen.configs.Constant;

import java.io.File;

public class SystemActivityUtils {

    private static SystemActivityUtils mSystemActivityUtils = null;

    private SystemActivityUtils() {
    }

    public static SystemActivityUtils getInstance() {
        if (mSystemActivityUtils == null) {
            synchronized (SystemActivityUtils.class) {
                if (mSystemActivityUtils == null) {
                    mSystemActivityUtils = new SystemActivityUtils();
                }
            }
        }
        return mSystemActivityUtils;
    }

    /**
     * open application list activity
     *
     * @param context context
     */
    public void startAppListActivity(Context context) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * open the album activity
     *
     * @param activity    activity
     * @param requestCode request code
     */
    public void startAlbumActivity(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * open the album video activity
     *
     * @param activity    activity
     * @param requestCode request code
     */
    public void startAlbumVideoActivity(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("video/*");
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * uninstall other App
     *
     * @param activity    activity
     * @param packageName packageName
     * @param requestCode requestCode
     */
    public void uninstallOtherApp(Activity activity, String packageName, int requestCode) {
        Uri uri = Uri.fromParts("package", packageName, null);
        Intent intent = new Intent(Intent.ACTION_DELETE, uri);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * @param activity    activity
     * @param requestCode request code
     * @return filePath save image file path
     */
    public String startCameraActivity(Activity activity, int requestCode) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = FileUtils.getInstance().createImageFile(activity);
            } catch (Exception ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri uriForFile = FileProvider.getUriForFile(activity, Constant.fileProviderPath, photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
                activity.startActivityForResult(takePictureIntent, requestCode);
                return photoFile.getAbsolutePath();
            }
        }
        return "";
    }

    /**
     * notify system scan image file
     *
     * @param activity activity
     * @param filePath file path
     */
    public void notifySystemScanImageFile(Activity activity, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File file = new File(filePath);
        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        activity.sendBroadcast(mediaScanIntent);
    }
}
